package bupt.party.online.video;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class VideoPool implements IVideoPool{
	private Map<String, Video> videoMap = new TreeMap<String, Video>();
	private Map<String, Video> sortedVideoMap = new TreeMap<String, Video>();
	private List<Video> sortedVideoList = new ArrayList<Video>();

	@Override
	public List<Video> getVideosByCategoryId(String categoryId) {
		List<Video> list = new ArrayList<Video>();
		
		if(categoryId == null || categoryId.equals("")){
			return list;
		}
		
		Iterator<Video> iter = sortedVideoList.iterator();
		while(iter.hasNext()){
			Video item = iter.next();
			String itemCategoryId = item.getProps().getProperty(Video.CATEGORY_ID);
//			System.out.println("itemCategoryId: " + itemCategoryId);
			if(itemCategoryId.startsWith(categoryId)){
				list.add(item);
			}
		}
		
		return list;
	}

	/**
	 * Status: 0, 1, 2
	 * 0: UnEdited
	 * 1: UnPublished
	 * 2: Published
	 */
	@Override
	public List<Video> getVideosByStatus(String status) {
		List<Video> list = new ArrayList<Video>();
		if(status == null || status.length() == 0){
			return list;
		}
		
		if(!status.equals("0") && !status.equals("1") && !status.equals("2")){
			return list;
		}
		
		Iterator<Video> iter = sortedVideoList.iterator();
		while(iter.hasNext()){
			Video item = iter.next();
			String itemStatus = item.getProps().getProperty(Video.STATUS, "-1");
			if(status.equals(itemStatus)){
				list.add(item);
			}
		}
		
		return list;
	}
	
	/**
	 * 根据categoryId和status获取符合要求的视频
	 */
	@Override
	public List<Video> getVideos(String categoryId, String status) {
		List<Video> orgVideoList = this.getVideosByCategoryId(categoryId);
		List<Video> list = new ArrayList<Video>();
		if(status == null || status.length() == 0){
			return list;
		}
		
		if(!status.equals("0") && !status.equals("1") && !status.equals("2")){
			return list;
		}
		
		Iterator<Video> iter = orgVideoList.iterator();
		while(iter.hasNext()){
			Video item = iter.next();
			String itemStatus = item.getProps().getProperty(Video.STATUS, "-1");
			if(status.equals(itemStatus)){
				list.add(item);
			}
		}
		
		return list;
	}
	
	@Override
	public List<Video> getAllVideos() {
		List<Video> list = new ArrayList<Video>();
		Iterator<Video> iter = sortedVideoList.iterator();
		while(iter.hasNext()){
			list.add(iter.next());
		}
		
		return list;
	}

	@Override
	public void clear() {
		videoMap.clear();
	}

	@Override
	public void addVideo(Video item) {
		videoMap.put(item.getVideopath(), item);
	}

	@Override
	public boolean updateVideoInfo(String videoId, String categoryId, String name,  
			String picPath, String introduction, String videoStatus1) {
		if(videoId == null || videoId.equals("")){
			return false;
		}
		
		Iterator<Video> iter = videoMap.values().iterator();
		while(iter.hasNext()){
			Video item = iter.next();
			if(videoId.equals(item.getVideopath())){
				if(picPath != null && !picPath.equals("")){
					item.setPicPath(picPath);
				}
				if(name != null && !name.equals("")){
					item.getProps().setProperty(Video.NAME, name);
				}
				if(introduction != null && !introduction.equals("")){
					item.getProps().setProperty(Video.INTRODUCTION, introduction);
				}
				if(videoStatus1 == null || videoStatus1.length() == 0)
					videoStatus1 = "1";
				item.getProps().setProperty(Video.STATUS, videoStatus1);
				item.store();
				return true;
			}
		}
		updateSortedVideoMap();
		return false;
	}
	
	public boolean swapVideosUploadDate(String videoPath1, String videoPath2){
		if(videoPath1 == null || videoPath1.equals("") || videoPath2 == null || videoPath2.equals("")){
			return false;
		}
		Video video1 = null;
		Video video2 = null;
		
		Iterator<Video> iter = sortedVideoList.iterator();
		while(iter.hasNext()){
			Video item = iter.next();
			if(videoPath1.equals(item.getVideopath())){
				video1 = item;
				break;
			}
		}
		while(iter.hasNext()){
			Video item = iter.next();
			if(videoPath2.equals(item.getVideopath())){
				video2 = item;
				break;
			}
		}
		if(video1 == null || video2 == null){
			return false;
		}
		String tempDateStr = video1.getProps().getProperty(Video.UPLOAD_DATE);
		video1.getProps().setProperty(Video.UPLOAD_DATE, video2.getProps().getProperty(Video.UPLOAD_DATE));
		video2.getProps().setProperty(Video.UPLOAD_DATE, tempDateStr);
		video1.store();
		video2.store();
		this.updateSortedVideoMap();
		return true;
	}
	
	public boolean moveStep(String videoPath, int step){
//		sortedVideoList
		if(videoPath == null){
			return false;
		}
		
		int targetIndex = 0;
		for(; targetIndex < sortedVideoList.size(); targetIndex++){
			if(videoPath.equals(sortedVideoList.get(targetIndex).getVideopath())){
				break;
			}
		}
		if(targetIndex == sortedVideoList.size()){
			return false;
		}
		
		if(step > 0 && targetIndex > 0){
			String toVideoPath = sortedVideoList.get(targetIndex - step).getVideopath();
			return swapVideosUploadDate(toVideoPath, videoPath);
		}
		
		if(step < 0 && targetIndex < (sortedVideoList.size() - 1)){
			String toVideoPath = sortedVideoList.get(targetIndex - step).getVideopath();
			return swapVideosUploadDate(videoPath, toVideoPath);
		}
		return false;
	}
	
	public Video getVideo(String videoPic){
		if(videoPic == null || videoPic.equals("")){
			return null;
		}
		
		Iterator<Video> iter = sortedVideoMap.values().iterator();
		while(iter.hasNext()){
			Video item = iter.next();
			if(videoPic.equals(item.getVideopath())){
				return item;
			}
		}
		return null;
	}
	
	
	public void updateSortedVideoMap(){
		sortedVideoMap.clear();
		sortedVideoList.clear();
		Iterator<Video> iter = videoMap.values().iterator();
		while(iter.hasNext()){
			Video item = iter.next();
			sortedVideoMap.put(item.getProps().getProperty(Video.UPLOAD_DATE), item);
		}
		iter = sortedVideoMap.values().iterator();
		while(iter.hasNext()){
			sortedVideoList.add(iter.next());
		}
		int len = sortedVideoList.size() - 1;
		for(int i = 0; i < len/2; i++){
			Video temp = sortedVideoList.get(i);
			sortedVideoList.set(i, sortedVideoList.get(len - i));
			sortedVideoList.set(len - i, temp);
		}
	}

	@Override
	public Video getLastestVideo() {
		if(sortedVideoList.size() == 0){
			return null;
		}
		return sortedVideoList.get(0);
	}

	@Override
	public List<Video> getHotestVideos() {

		return sortedVideoList;
	}

	@Override
	public List<Video> getLastest3Video() {
		List<Video> videoList = new ArrayList<Video>();
		
		Iterator<VideoMenuItem> iter = VideoMenu.getInstance().getMenuList().iterator();
		while(iter.hasNext()){
			VideoMenuItem vmi = iter.next();
			if(videoList.size() >= 3){
				break;
			}
			
			String path = "/" + vmi.getPath();
			String categoryName = vmi.getName();
			List<Video> tempVideoList = this.getVideos(path, "2");
			if(tempVideoList == null || tempVideoList.size() == 0){
				continue;
			}
			Video tempVideo = tempVideoList.get(0);
			tempVideo.getProps().setProperty(Video.CATEGORY_NAME, categoryName);
			videoList.add(tempVideo);
		}
		return videoList;
	}
}
